package com.yicong.ants.manager.ad;

import android.app.Activity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.cchao.simplelib.core.ImageLoader;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.util.StringHelper;
import com.yicong.ants.bean.ad.AdBanner;
import com.yicong.ants.bean.ad.AntsAd;
import com.yicong.ants.manager.RouterHelper;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * @author cch
 * @version 2021/7/13
 */
public class AntsAdHelper {

    // 广告位
    public interface Position {
        String app_index_mall_product = "app_index_mall_product";
        String scenic_product_banner = "scenic_product_banner";
        String mall_banner = "mall_banner";
        String mall_index = "mall_index";
        String circle_ad = "circle_ad";
        String ant_daily_task = "ant_daily_task";
        String energy_task_mine = "energy_task_mine"; // 我的页面
        String energy_task_trans = "energy_task_trans"; // 转赠页面
        String energy_task_web = "energy_task_web"; // 活动页
        String All = "all";
    }

    private static AntsAd mAdInfo = null;

    public static AntsAd getAdInfo(String position) {
        if (mAdInfo == null) {
            return null;
        }

        if (position.equals(Position.All)
            || position.equals(mAdInfo.getPosition())) {
            return mAdInfo;
        }

        return null;
    }

    public static void clearAdInfo(String position) {
        if (mAdInfo == null) {
            return;
        }

        if (position.equals(mAdInfo.getPosition())
            || position.equals(Position.All)) {
            mAdInfo = null;
        }
    }

    public static void setAdInfo(AntsAd mAdInfo, String position) {
        AntsAdHelper.mAdInfo = mAdInfo;
        mAdInfo.setPosition(position);
    }

    /**
     * 广告曝光上发
     */
    public static void exposeAd(String ad_ids) {
        if (StringHelper.isEmpty(ad_ids)) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("ad_id", ad_ids);
        Disposable subscribe = RetrofitHelper.getApis().adExpose(map)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                if (respBean.isCodeFail()) {
                    UiHelper.showToast(respBean.getMsg());
                    return;
                }
            }, RxHelper.getErrorConsumer());
    }

    /**
     * 广告点击上发
     */
    public static void adClicked(int ad_id) {
        if (ad_id <= 0) {
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("ad_id", String.valueOf(ad_id));
        Disposable subscribe = RetrofitHelper.getApis().adClicked(map)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                if (respBean.isCodeFail()) {
                    UiHelper.showToast(respBean.getMsg());
                    return;
                }
            }, RxHelper.getErrorConsumer());
    }

    public static View getAdBanner(Activity context, AdBanner banner) {
        ImageView imageView = new ImageView(context);

        int height = Math.round(UiHelper.getScreenWidth() * 1.0f / banner.getWidth() * banner.getHeight());
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(UiHelper.getScreenWidth(), height);
        ImageLoader.loadImage(imageView, banner.getDisplay());
        imageView.setLayoutParams(layoutParams);

        imageView.setOnClickListener(v -> {
            AntsAdHelper.adClicked(Integer.parseInt(banner.getAd_id()));
            RouterHelper.router(context, banner.getUrl());
        });
        return imageView;
    }
}
